import { _decorator, Component, input, Input, Label, Node, Collider, director } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('NewComponent')
export class NewComponent extends Component {
    // 控制文本组件
    @property(Label)
    Tips_Label: Label = null

    // 控制节点（2D/3D）显示/隐藏/销毁
    @property(Node) // 提示框父节点
    Tips_Node: Node = null

    // 脚本中可以绑定节点/组件 @property(类型) 对象名: 类型 = null
    @property(Node)
    C_Node: Node = null

    @property(Node) // 赛车节点
    Player_Node: Node = null

    @property  // 可以使该变量在编辑器中编辑
    Player_Speed: number = 30; // 设置角色速度

    Player_Move: { a: boolean, d: boolean } = { a: false, d: false } // 角色是否在移动

    Player_Collider: Collider = null // 赛车自身节点的碰撞组件

    Move = true  // 移动开关
    protected onLoad(): void {
        input.on(Input.EventType.KEY_DOWN, this.keyDown, this);
        input.on(Input.EventType.KEY_UP, this.keyUp, this);
        // 获取赛车节点的碰撞组件
        this.Player_Collider = this.Player_Node.getComponent(Collider)
        // 监听赛车节点的碰撞事件
        this.Player_Collider.on('onTriggerEnter', this.Start_Collider, this)
    }

    protected onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.keyDown, this)
        input.off(Input.EventType.KEY_UP, this.keyUp, this)
        this.Player_Collider.off('onTriggerEnter', this.Start_Collider, this) // 关闭监听
    }

    Start_Collider(Event) { // 碰撞后的执行函数

        this.Move = false // 碰撞时 停止移动赛车
        this.Tips_Node.active = true // false隐藏 true显示
        if (Event.otherCollider.node.name === 'Win') {
            this.Tips_Label.string = '通关啦！！！'
            console.log('游戏成功')
        } else {
            this.Tips_Label.string = '游戏失败！'
            console.log('游戏失败')
        }

    }

    New_Game() {
        // this.Tips_Node.active = false
        // this.node.setPosition(-1.953, 0, -1)
        // this.C_Node.setPosition(0, 19.51, 26.623)
        // this.Move = true

        director.loadScene('C1')  // 加载场景
    }

    keyDown(e) {
        // console.log("按下了")
        if (e.keyCode === 65) {
            this.Player_Move.a = true
            // console.log('按下了A')
        }
        if (e.keyCode === 68) {
            this.Player_Move.d = true
            // console.log('按下了D')
        }
    }

    keyUp(e) {
        if (e.keyCode === 65) {
            // console.log('释放了A')
            this.Player_Move.a = false
        }
        if (e.keyCode === 68) {
            // console.log('释放了D')
            this.Player_Move.d = false
        }
    }

    start() {

    }

    update(deltaTime: number) {
        if (!this.Move) return



        // deltaTime: 每一帧的时间
        const pos = this.node.getPosition();

        // if(pos.z <= -246.5){
        //     this.Move = false,
        //     console.log('成功通过了关卡')
        // }

        const C_Pos = this.C_Node.getPosition();

        const deltaData = this.Player_Speed * deltaTime // 每一帧移动的距离
        if (this.Player_Move.a && !this.Player_Move.d) {
            // x向左移动
            pos.x = pos.x - deltaData;
        } else if (this.Player_Move.d && !this.Player_Move.a) {
            // x向右移动
            pos.x = pos.x + deltaData;
        }

        pos.x = Math.max(Math.min(pos.x, 3.3), -3.3)
        this.node.setPosition(pos.x, pos.y, pos.z - deltaData); // 每秒钟移动1个单位
        this.C_Node.setPosition(C_Pos.x, C_Pos.y, C_Pos.z - deltaData);
    }
}


